package es.ats.codington.festival.daos;



import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;


import es.ats.codington.festival.Mapper.EventMapper;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.exceptions.DAOSExceptions;
import es.ats.codington.festival.interfaces.daos.iEventSignUpDAO;
import es.ats.codington.festival.utils.FERSDataConnection.FersDC;
import es.ats.codington.festival.utils.Query.JSonQuery;


/**
 * The Class EventSignUpDAOImpl.
 */
public class EventSignUpDAOImpl implements iEventSignUpDAO {
	
	/** The jdbc template object. */
	private JdbcTemplate jdbcTemplateObject;
	
	/** The query properties. */
	private Properties queryProperties;
	
	/**
	 * Instantiates a new event sign up dao impl.
	 *
	 * @param jdbcTemplateObject the jdbc template object
	 * @param queryProperties the query properties
	 */
	public EventSignUpDAOImpl(JdbcTemplate jdbcTemplateObject,Properties queryProperties ) {
		this.jdbcTemplateObject = jdbcTemplateObject;
		this.queryProperties = queryProperties;
	} 


	
	/**
	 * Instantiates a new event sign up dao impl.
	 */
	public EventSignUpDAOImpl() {
		super();
	}
	
	/**
	 * Sign up to event.
	 *
	 * @param iDvisitor the i dvisitor
	 * @param iDevent the i devent
	 * @return boolean
	 * @throws Exception the exception
	 */
	@Override
	public boolean signUpToEvent(final int iDvisitor, final int iDevent)throws Exception {

		//Modifying the seats available
		List<Event> events = jdbcTemplateObject.query(queryProperties.getProperty("getidevent"),new PreparedStatementSetter(){
			
			@Override
			public void setValues(PreparedStatement statement)throws SQLException{
				statement.setInt(1,iDevent);
			}
		}, new EventMapper());
		
		 int seats= events.get(0).getSeats();
		if (seats==0){
			throw new Exception("No more available seats");				
		}else{
			seats=seats -1;
		}
		final int seats2 = seats;
		
		int affectedRows2=0;
		
		affectedRows2 = jdbcTemplateObject.update(queryProperties.getProperty("updateseats"),new PreparedStatementSetter(){
			
			@Override
			public void setValues(PreparedStatement statement)throws SQLException{
				statement.setInt(1, seats2);
				statement.setInt(2, iDevent);
			}
		});			
					
		// Doing the Sign Up.
		int affectedRows=0;
		
		affectedRows = jdbcTemplateObject.update(queryProperties.getProperty("signuptoevent"),new PreparedStatementSetter(){
			
			@Override
			public void setValues(PreparedStatement statement)throws SQLException{
				statement.setInt(1, iDvisitor);
				statement.setInt(2, iDevent);
			}
		});		

		if(affectedRows!=0){
			return true;
		}else{
		return false;
		}
		
	}


	/**
	 * Unsign up to event.
	 *
	 * @param iDvisitor the i dvisitor
	 * @param iDevent the i devent
	 * @return true, if successful
	 * @throws Exception the exception
	 */
	@Override
	public boolean unsignUpToEvent(final int iDvisitor, final int iDevent)throws Exception {
		
			
			// Doing the UnSign Up.
			int affectedRows=0;
			
			affectedRows = jdbcTemplateObject.update(queryProperties.getProperty("unsignuptoevent"),new PreparedStatementSetter(){
				
				@Override
				public void setValues(PreparedStatement statement)throws SQLException{
					statement.setInt(1, iDvisitor);
					statement.setInt(2, iDevent);
				}
			});	
			
			//Modifying the seats available
			List<Event> events = jdbcTemplateObject.query(queryProperties.getProperty("getidevent"),new PreparedStatementSetter(){
				
				@Override
				public void setValues(PreparedStatement statement)throws SQLException{
					statement.setInt(1,iDevent);
				}
			}, new EventMapper());
			
			 int seats= events.get(0).getSeats();
			seats=seats +1;
			
			final int seats2 = seats;
			
			int affectedRows2=0;
			
			affectedRows2 = jdbcTemplateObject.update(queryProperties.getProperty("updateseats"),new PreparedStatementSetter(){
				
				@Override
				public void setValues(PreparedStatement statement)throws SQLException{
					statement.setInt(1, seats2);
					statement.setInt(2, iDevent);
				}
			});						
			
		if(affectedRows!=0){
		return true;
		}
		else{
		return false;
		}
	}

	
	

} 

